<?php

/**
 *
 *	@author Xiaoting <[<yichen.ye@zhaopin.com.cn>]>
 *	@date(2017.9.18) 创建
 * 
 */
class Wafrules_model extends CI_Model{

	function __construct()
	{
		$this->load->helper('date');
		$this->load->database();
		$this->load->library('session');
	}

	/**
	 * 获取所有的用户组列表
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function wafrule_list($inputs)
	{
		$this->db->select('ZL_WAFRULE_ID');
		$zl_group_count = $this->db->get('zl_wafrule')->num_rows();

		$this->db->select('ZL_WAFRULE_ID id,ZL_WAFRULE_TIME time,ZL_WAFRULE_NAME name,ZL_WAFRULE_TYPE type,
			ZL_WAFRULE_USER user, ZL_WAFRULE_STATUS status');
		$this->db->limit($inputs['limit'],$inputs['offset']);
		$zl_group = $this->db->get('zl_wafrule');

		return array(
			'status' => 1,
			'data' => $zl_group->result_array(),
			'total' => $zl_group_count
		);
	}		

	/**
	 * 添加用户组信息
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function wafrule_add($inputs)
	{
		//定义zl_wafrule插入的数据
		$id = 'ZL-WAFRULR-'. date("Ymd-His");
		$data = array(
			'ZL_WAFRULE_ID' => $id,
			'ZL_WAFRULE_NAME' => $inputs['name'],
			'ZL_WAFRULE_TIME' => $this->comon->_datetime(),
			'ZL_WAFRULE_TYPE' => $inputs['type'],
			'ZL_WAFRULE_USER' => $this->session->userdata('username'),
			'ZL_WAFRULE_STATUS' => 0
		);

		//定义插入的正则规则
		$data_reg = array();
		if (strlen($inputs['reg']) > 0){
			if(strlen($inputs['reg']) < 64){
				$data_reg = array(
					'ZL_WAFRULE_ATTR_KEY' => 'reg',
					'ZL_WAFRULE_ATTR_VALUE' => $inputs['reg'],
					'ZL_WAFRULE_ATTR_RULEID' => $id
			 	);
			}
			else{
				$data_reg = array(
					'ZL_WAFRULE_ATTR_CONTENT_KEY' => 'reg',
					'ZL_WAFRULE_ATTR_CONTENT_VALUE' => $inputs['reg'],
					'ZL_WAFRULE_ATTR_CONTENT_RULEID' => $id
			 	);
			}
		}
		
	
		//定义插入的IP规则
		$clientip = substr($inputs['clientip'],1,strlen($inputs['clientip'])-1);
		$clientips = explode(',', $clientip);
		foreach( $clientips as $k=>$v){  
    		if( !$v )  
        		unset( $clientips[$k] );  
		}   

		$data_clientip= array();
		if (count($clientips) != 0){
			if (count($clientips) > 1) {
				foreach ($clientips as $key => $value) {
					//判断是否是IP		
					if($this->comon->_boolval($value,"ip")){
						array_push($data_clientip,
							array('ZL_WAFRULE_ATTR_KEY' => 'clientip', 
								'ZL_WAFRULE_ATTR_VALUE' => $value,
								'ZL_WAFRULE_ATTR_RULEID' => $id
							)
						);
					}
					else {
						return array('status' => -1);//status等于-1时，前端提示“请填写正确的IP”
					}
					
				}
			}
			else if(count($clientips)==1 && $this->comon->_boolval(end($clientips),"ip")){ 
				$data_clientip = array(
					'ZL_WAFRULE_ATTR_KEY' => 'clientip',
					'ZL_WAFRULE_ATTR_VALUE' => end($clientips),
					'ZL_WAFRULE_ATTR_RULEID' => $id
				);
			}
			else{
				return array('status' => -1);//status等于-1时，前端提示“请填写正确的IP”
			}
		}
		
		

		//定义插入的referer规则
		$referer = substr($inputs['referer'],1,strlen($inputs['referer'])-1);
		$referers = explode(',', $referer);

		foreach( $referers as $k=>$v){  
    		if( !$v )  
        		unset( $referers[$k] );  
		} 

		$data_referer = array();
		if (count($referers) != 0){
			if (count($referers) > 1) {
				foreach ($referers as $key => $value) {
					array_push($data_referer,
						array('ZL_WAFRULE_ATTR_CONTENT_KEY' => 'referer', 
							'ZL_WAFRULE_ATTR_CONTENT_VALUE' => $value,
							'ZL_WAFRULE_ATTR_CONTENT_RULEID' => $id
						)
					);
				}
			}
			else{ 
				$data_referer = array(
					'ZL_WAFRULE_ATTR_CONTENT_KEY' => 'referer',
					'ZL_WAFRULE_ATTR_CONTENT_VALUE' => end($referers),
					'ZL_WAFRULE_ATTR_CONTENT_RULEID' => $id
				);
			}
		}	

		//定义插入的useragent规则
		$useragent = substr($inputs['useragent'],1,strlen($inputs['useragent'])-1);			
		$useragents = explode(',', $useragent);
		foreach( $useragents as $k=>$v){  
    		if( !$v )  
        		unset( $useragents[$k] );  
		} 

		$data_useragent = array();
		if (count($useragents) != 0){
			if (count($useragents) > 1) {
				foreach ($useragents as $key => $value) {
					if ($value!='') {
						array_push($data_useragent,
							array('ZL_WAFRULE_ATTR_CONTENT_KEY' => 'useragent', 
								'ZL_WAFRULE_ATTR_CONTENT_VALUE' => $value,
								'ZL_WAFRULE_ATTR_CONTENT_RULEID' => $id
							)
						);
					}
				}
			}
			else{ 
				$data_useragent = array(
					'ZL_WAFRULE_ATTR_CONTENT_KEY' => 'useragent',
					'ZL_WAFRULE_ATTR_CONTENT_VALUE' => end($useragents),
					'ZL_WAFRULE_ATTR_CONTENT_RULEID' => $id
				);
			}
		}

		//定义插入攻击的规则状态
		$data_status = array(
				'ZL_WAFRULE_ATTR_KEY' => 'status',
				'ZL_WAFRULE_ATTR_VALUE' => $inputs['status'],
				'ZL_WAFRULE_ATTR_RULEID' => $id
		 );

		$this->db->trans_begin();
		$return_date = $this->db->insert('zl_wafrule',$data);
		if (count($data_reg) > 0){
			if(strlen($inputs['reg']) < 64){
				$return_reg = $this->db->insert('zl_wafrule_attr',$data_reg);
			}
			else{
				$return_reg = $this->db->insert('zl_wafrule_attr_content',$data_reg);
			}
		}
		else{
			return array('status' => -2);//返回为-2时，前端提示“正则表达式不能为空”
		}

		if (count($data_clientip) > 0){
			if (count($clientips) > 1) {
				$return_clientip = $this->db->insert_batch('zl_wafrule_attr',$data_clientip);
			}
			else{
				$return_clientip = $this->db->insert('zl_wafrule_attr',$data_clientip);
			}
		}
		else{
			$return_clientip = 1;
		}


		if (count($data_referer) > 0){
			if (count($referers) > 1) {
				$return_referer = $this->db->insert_batch('zl_wafrule_attr_content',$data_referer);
			}
			else{
				$return_referer = $this->db->insert('zl_wafrule_attr_content',$data_referer);
			}
		}
		else{
			$return_referer = 1;
		}

		if (count($data_useragent) > 0){
			if (count($useragents) > 1) {
				$return_useragent = $this->db->insert_batch('zl_wafrule_attr_content',$data_useragent);
			}
			else{
				$return_useragent = $this->db->insert('zl_wafrule_attr_content',$data_useragent);
			}
		}
		else{
			$return_useragent = 1;
		}

		$return_status = $this->db->insert('zl_wafrule_attr',$data_status);
		
		if ($return_date && $return_reg && $return_clientip && $return_referer && $return_useragent && $return_status){
			$this->db->trans_commit();
			return array('status' => 1);
		}
		else{
			$this->db->trans_rollback();
			return array('status' => 0);
		}

	}

	/**
	 * 修改指定用户组信息
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 * @author xiaoting 
	 */
	function wafrulechange_read($inputs)
	{
		$this->db->select('ZL_WAFRULE_ATTR_KEY rkey,ZL_WAFRULE_ATTR_VALUE val');
		$this->db->where('ZL_WAFRULE_ATTR_RULEID', $inputs['id']);
		$zl_wafrule_attr = $this->db->get('zl_wafrule_attr');

		$this->db->select('ZL_WAFRULE_ATTR_CONTENT_KEY ckey,ZL_WAFRULE_ATTR_CONTENT_VALUE val');
		$this->db->where('ZL_WAFRULE_ATTR_CONTENT_RULEID', $inputs['id']);
		$zl_wafrule_attr_content = $this->db->get('zl_wafrule_attr_content');

		$this->db->select('ZL_WAFRULE_ID id,ZL_WAFRULE_TIME time,ZL_WAFRULE_NAME name');
		$this->db->where('ZL_WAFRULE_ID', $inputs['id']);
		$zl_wafrule = $this->db->get('zl_wafrule');
		return array(
			'status' => 1,
			'data' => array(
				'wafrule' => $zl_wafrule->result_array(),
				'wafrule_attr_content' => $zl_wafrule_attr_content->result_array(),
				'wafrule_attr' => $zl_wafrule_attr->result_array() 
			)
		);
	}

	/**
	 * 确认修改指定用户组信息
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 * @author xiaoting 
	 */
	function wafrule_change($inputs)
	{
		$data_update = array(
			'ZL_WAFRULE_TIME' => $this->comon->_datetime(),
			'ZL_WAFRULE_USER' => $this->session->userdata('username'),
			'ZL_WAFRULE_NAME' => $inputs['name']
		);

		//定义插入的正则规则
		$data_reg = array();
		if (strlen($inputs['reg']) > 0){
			if(strlen($inputs['reg']) < 64){
				$data_reg = array(
					'ZL_WAFRULE_ATTR_KEY' => 'reg',
					'ZL_WAFRULE_ATTR_VALUE' => $inputs['reg'],
					'ZL_WAFRULE_ATTR_RULEID' => $inputs['id']
			 	);
			}
			else{
				$data_reg = array(
					'ZL_WAFRULE_ATTR_CONTENT_KEY' => 'reg',
					'ZL_WAFRULE_ATTR_CONTENT_VALUE' => $inputs['reg'],
					'ZL_WAFRULE_ATTR_CONTENT_RULEID' => $inputs['id']
			 	);
			}
		}
		
	
		//定义插入的IP规则
		$clientip = substr($inputs['clientip'],1,strlen($inputs['clientip'])-1);
		$clientips = explode(',', $clientip);
		foreach( $clientips as $k=>$v){  
    		if( !$v )  
        		unset( $clientips[$k] );  
		}   

		$data_clientip= array();
		if (count($clientips) != 0){
			if (count($clientips) > 1) {
				foreach ($clientips as $key => $value) {
					//判断是否是IP		
					if($this->comon->_boolval($value,"ip")){
						array_push($data_clientip,
							array('ZL_WAFRULE_ATTR_KEY' => 'clientip', 
								'ZL_WAFRULE_ATTR_VALUE' => $value,
								'ZL_WAFRULE_ATTR_RULEID' => $inputs['id']
							)
						);
					}
					else {
						return array('status' => -1);//status等于-1时，前端提示“请填写正确的IP”
					}
					
				}
			}
			else if(count($clientips)==1 && $this->comon->_boolval(end($clientips),"ip")){ 
				$data_clientip = array(
					'ZL_WAFRULE_ATTR_KEY' => 'clientip',
					'ZL_WAFRULE_ATTR_VALUE' => end($clientips),
					'ZL_WAFRULE_ATTR_RULEID' => $inputs['id']
				);
			}
			else{
				return array('status' => -1);//status等于-1时，前端提示“请填写正确的IP”
			}
		}
		
		

		//定义插入的referer规则
		$referer = substr($inputs['referer'],1,strlen($inputs['referer'])-1);
		$referers = explode(',', $referer);

		foreach( $referers as $k=>$v){  
    		if( !$v )  
        		unset( $referers[$k] );  
		} 

		$data_referer = array();
		if (count($referers) != 0){
			if (count($referers) > 1) {
				foreach ($referers as $key => $value) {
					array_push($data_referer,
						array('ZL_WAFRULE_ATTR_CONTENT_KEY' => 'referer', 
							'ZL_WAFRULE_ATTR_CONTENT_VALUE' => $value,
							'ZL_WAFRULE_ATTR_CONTENT_RULEID' => $inputs['id']
						)
					);
				}
			}
			else { 
				$data_referer = array(
					'ZL_WAFRULE_ATTR_CONTENT_KEY' => 'referer',
					'ZL_WAFRULE_ATTR_CONTENT_VALUE' => current($referers),
					'ZL_WAFRULE_ATTR_CONTENT_RULEID' => $inputs['id']
				);
			}
		}	

		//定义插入的useragent规则
		$useragent = substr($inputs['useragent'],1,strlen($inputs['useragent'])-1);			
		$useragents = explode(',', $useragent);
		foreach( $useragents as $k=>$v){  
    		if( !$v )  
        		unset( $useragents[$k] );  
		} 

		$data_useragent = array();
		if (count($useragents) != 0){
			if (count($useragents) > 1) {
				foreach ($useragents as $key => $value) {
					if ($value!='') {
						array_push($data_useragent,
							array('ZL_WAFRULE_ATTR_CONTENT_KEY' => 'useragent', 
								'ZL_WAFRULE_ATTR_CONTENT_VALUE' => $values,
								'ZL_WAFRULE_ATTR_CONTENT_RULEID' => $inputs['id']
							)
						);
					}
				}
			}
			else{ 
				$data_useragent = array(
					'ZL_WAFRULE_ATTR_CONTENT_KEY' => 'useragent',
					'ZL_WAFRULE_ATTR_CONTENT_VALUE' => end($useragents),
					'ZL_WAFRULE_ATTR_CONTENT_RULEID' => $inputs['id']
				);
			}
		}
		

		//定义插入攻击的规则状态
		$data_status = array(
				'ZL_WAFRULE_ATTR_KEY' => 'status',
				'ZL_WAFRULE_ATTR_VALUE' => $inputs['status'],
				'ZL_WAFRULE_ATTR_RULEID' => $inputs['id']
		 );

		$this->db->trans_begin();
		$this->db->where('ZL_WAFRULE_ID', $inputs['id']);
		$return_update = $this->db->update('zl_wafrule',$data_update);
		$return_del_attr = $this->db->delete('zl_wafrule_attr', array('ZL_WAFRULE_ATTR_ID' => $inputs['id']));
		$return_del_content = $this->db->delete('zl_wafrule_attr_content', array('ZL_WAFRULE_ATTR_CONTENT_RULEID' => $inputs['id']));

		if (count($data_reg) > 0){
			if(strlen($inputs['reg']) < 64){
				$return_reg = $this->db->insert('zl_wafrule_attr',$data_reg);
			}
			else{
				$return_reg = $this->db->insert('zl_wafrule_attr_content',$data_reg);
			}
		}
		else{
			return array('status' => -2);//返回为-2时，前端提示“正则表达式不能为空”
		}

		if (count($data_clientip) > 0){
			if (count($clientips) > 1) {
				$return_clientip = $this->db->insert_batch('zl_wafrule_attr',$data_clientip);
			}
			else{
				$return_clientip = $this->db->insert('zl_wafrule_attr',$data_clientip);
			}
		}
		else{
			$return_clientip = 1;
		}


		if (count($data_referer) > 0){
			if (count($referers) > 1) {
				$return_referer = $this->db->insert_batch('zl_wafrule_attr_content',$data_referer);
			}
			else{
				$return_referer = $this->db->insert('zl_wafrule_attr_content',$data_referer);
			}
		}
		else{
			$return_referer = 1;
		}

		if (count($data_useragent) > 0){
			if (count($useragents) > 1) {
				$return_useragent = $this->db->insert_batch('zl_wafrule_attr_content',$data_useragent);
			}
			else{
				$return_useragent = $this->db->insert('zl_wafrule_attr_content',$data_useragent);
			}
		}
		else{
			$return_useragent = 1;
		}

		$return_status = $this->db->insert('zl_wafrule_attr',$data_status);
		
		if ($return_update && $return_del_attr && $return_del_content && $return_reg && $return_clientip && $return_referer && $return_useragent && $return_status){
			$this->db->trans_commit();
			return array('status' => 1);
		}
		else{
			$this->db->trans_rollback();
			return array('status' => 0);
		}
	}
	
	/**
	 * 切换WAF规则状态
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function wafrulesStatus_change($inputs)
	{
		$session =  $this->session->userdata('username');
		$data = array(
			'ZL_WAFRULE_TIME' => $this->comon->_datetime(),
			'ZL_WAFRULE_USER' => $session,
			'ZL_WAFRULE_STATUS' => $inputs["status"],
		);
		$this->db->where('ZL_WAFRULE_ID', $inputs['id']);
		$zl_wafrule = $this->db->update('zl_wafrule',$data);
		if (!$zl_wafrule){
			return array('status' => 0);
		}
		return array('status' => 1);
	}

	/**
	 * WAF规则删除
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function wafrule_del($inputs)
	{
		$this->db->where('ZL_WAFRULE_ATTR_RULEID', $inputs['id']);
		$this->db->delete('zl_wafrule_attr');

		$this->db->where('ZL_WAFRULE_ATTR_CONTENT_RULEID', $inputs['id']);
		$this->db->delete('zl_wafrule_attr_content');

		$this->db->where('ZL_WAFRULE_ID', $inputs['id']);
		$this->db->delete('zl_wafrule');
		return array('status' => 1);
	}

	
	/**
	 * WAF规则读取详情
	 * @param  [type] $inputs [description]
	 * @return [type]         [description]
	 */
	function wafrule_read($inputs)
	{
		$this->db->select('ZL_WAFRULE_ATTR_KEY rkey,ZL_WAFRULE_ATTR_VALUE val');
		$this->db->where('ZL_WAFRULE_ATTR_RULEID', $inputs['id']);
		$zl_wafrule_attr = $this->db->get('zl_wafrule_attr');

		$this->db->select('ZL_WAFRULE_ATTR_CONTENT_KEY ckey,ZL_WAFRULE_ATTR_CONTENT_VALUE val');
		$this->db->where('ZL_WAFRULE_ATTR_CONTENT_RULEID', $inputs['id']);
		$zl_wafrule_attr_content = $this->db->get('zl_wafrule_attr_content');

		$this->db->select('ZL_WAFRULE_ID id,ZL_WAFRULE_TIME time,ZL_WAFRULE_NAME name,ZL_WAFRULE_TYPE type,
			ZL_WAFRULE_USER user, ZL_WAFRULE_STATUS status');
		$this->db->where('ZL_WAFRULE_ID', $inputs['id']);
		$zl_wafrule = $this->db->get('zl_wafrule');
		return array(
			'status' => 1,
			'data' => array(
				'wafrule' => $zl_wafrule->result_array(),
				'wafrule_attr_content' => $zl_wafrule_attr_content->result_array(),
				'wafrule_attr' => $zl_wafrule_attr->result_array() 
			)
		);
	}
	
}

?>
